package com.hwadee.teachingprocessmanagementsystem.service.base.impl;

import com.hwadee.teachingprocessmanagementsystem.dto.DepartmentDto;
import com.hwadee.teachingprocessmanagementsystem.entity.Department;
import com.hwadee.teachingprocessmanagementsystem.dao.DepartmentDao;
import com.hwadee.teachingprocessmanagementsystem.service.base.DepartmentService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import jakarta.annotation.Resource;

import java.util.List;

/**
 * 系部表(Department)表服务实现类
 *
 * @author makejava
 * @since 2024-07-11 14:20:03
 */
@Service("departmentService")
public class DepartmentServiceImpl implements DepartmentService {
    @Resource
    private DepartmentDao departmentDao;

    /**
     * 通过ID查询单条数据
     *
     * @param departmentId 主键
     * @return 实例对象
     */
    @Override
    public Department queryById(String departmentId) {
        return this.departmentDao.queryById(departmentId);
    }

    /**
     * 分页查询
     *
     * @param department 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Department> queryByPage(Department department, PageRequest pageRequest) {
        long total = this.departmentDao.count(department);
        return new PageImpl<>(this.departmentDao.queryAllByLimit(department, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param department 实例对象
     * @return 实例对象
     */
    @Override
    public Department insert(Department department) {
        this.departmentDao.insert(department);
        return department;
    }

    /**
     * 修改数据
     *
     * @param department 实例对象
     * @return 实例对象
     */
    @Override
    public Department update(Department department) {
        this.departmentDao.update(department);
        return this.queryById(department.getDepartmentId());
    }

    /**
     * 通过主键删除数据
     *
     * @param departmentId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String departmentId) {
        return this.departmentDao.deleteById(departmentId) > 0;
    }

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<Department> queryAll() {
        return this.departmentDao.queryAll();
    }

    /**
     * 获取系部数据传输对象
     * @return
     */
    @Override
    public DepartmentDto getDepartmentDto() {
        List<Department> departmentList = queryAll();
        DepartmentDto departmentDto = new DepartmentDto();
        departmentDto.setDepartmentList(departmentList);
        return departmentDto;
    }
}
